﻿using Hangfire;
using System;
using System.ServiceProcess;
using Yz.Base.Dal;
using Yz.Core.Tools;

namespace Yz.WinService.Hangfire
{
    /// <summary>
    /// 类下的依赖注入调用全部采用构造方法注入
    /// </summary>
    public partial class YzHfService : ServiceBase
    {
        private BackgroundJobServer _server;
        public YzHfService()
        {
            InitializeComponent();
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            GlobalConfiguration.Configuration.UseSqlServerStorage(DBConstant.HangfireConnStr);
        }
        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            if (e.ExceptionObject != null)
            {
                Exception ex = e.ExceptionObject as Exception;
                YzNLog.Fatal(ex);
            }
        }
        protected override void OnStart(string[] args)
        {
            _server = new BackgroundJobServer();
        }

        protected override void OnStop()
        {
            _server.Dispose();
        }
    }
}
